package com.hnkywl.sts.dao.organ;

import com.hnkywl.sts.dao.base.RegionDao;
import com.hnkywl.sts.entity.organ.Ejzh;
import com.hnkywl.sts.entity.organ.Khh;
import net.ruixin.orm.hibernate.Page;
import net.ruixin.orm.hibernate.SimpleHibernateTemplate;
import org.hibernate.Query;
import org.hibernate.criterion.CriteriaSpecification;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.hibernate.tool.hbm2x.StringUtils;
import org.hibernate.transform.AliasToEntityMapResultTransformer;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-3-23
 * Time: 下午1:37
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class EjzhDao extends SimpleHibernateTemplate<Ejzh, Long> {

    public Page<Ejzh> findEjzhList(Long nbgsId, String qc, String jc, int pageNo, int pageSize) {
        String hql = null;
        List<Object> param = new ArrayList<Object>();
        StringBuilder condition = new StringBuilder();
        Page<Ejzh> page = new Page<Ejzh>(pageNo, pageSize, 0);
        //==============条件拼接===============
        if (qc != null && !qc.isEmpty()) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" qc like ? ");
            param.add("%" + qc.trim() + "%");
        }
        if (jc != null && !jc.isEmpty()) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" jc like ? ");
            param.add("%" + jc.trim() + "%");
        }
        if (nbgsId != null) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" nbgs.id = ? ");
            param.add(nbgsId);
        }
        //==============记录数量===============
        hql = "select count(*) from Ejzh where 1=1  " + RegionDao.createDataRegion("and");
        if (condition.length() != 0)
            hql = hql + " and " + condition;
        Long c = null;
        if (param.size() != 0) {
            c = (Long) findUnique(hql, param.toArray());
            page.setTotalCount(c.intValue());
        } else {
            c = (Long) findUnique(hql);
            page.setTotalCount(c.intValue());
        }
        if (c == 0)
            return page;
        //==============查询结果===============
        hql = "from Ejzh where 1 = 1 " + RegionDao.createDataRegion("and");
        if (condition.length() != 0)
            hql = hql + " and " + condition;
        if (param.size() != 0)
            return find(page, hql, param.toArray());
        return find(page, hql);
    }

    /**
     * 判断岗位属性是否重复
     *
     * @param id            岗位id
     * @param propertyValue 岗位属性值
     * @param propertyName  岗位属性名
     * @return
     */
    public Boolean checkUnique(Long id, Object propertyValue, String propertyName) {
        String hql;
        if (propertyValue == null)
            return false;
        if (id == null) {
            //修改检验
            hql = "from Ejzh where " + propertyName + " = ?  ";
            return this.existsQuery(hql, propertyValue);
        } else {
            //新增检验
            hql = "from Ejzh where " + propertyName + " = ? and id <> ?  ";
            return this.existsQuery(hql, propertyValue, id);
        }
    }

    public Boolean checkKhUnique(Long gsId, Long erzhId, Long khId) {
        String hql;
        if (erzhId == null) {
            //修改检验
            hql = "from Ejzh where kh.id = ?  and nbgs.id = ?   ";
            return this.existsQuery(hql, khId, gsId);
        } else {
            hql = "from Ejzh where kh.id = ? and nbgs.id = ?  and id <> ?  ";
            return this.existsQuery(hql, khId, gsId, erzhId);
        }
    }


    public Ejzh findKyyeByKhId(Long khId, Long nbgsId) {
        //修改检验
        String hql = " from Ejzh where kh.id = ? and nbgs.id = ?  ";
        return this.find(hql, khId, nbgsId).size() == 0 ? null : (Ejzh) this.find(hql, khId, nbgsId).get(0);
    }

    public List<Ejzh> findEjzhByKhhId(Long KhhId) {
        //修改检验
        String hql = " from Ejzh where khh.id = ?  ";
        return (List<Ejzh>) this.find(hql, KhhId);
    }


    public List<Ejzh> findEjzhListByKhId(Long khId, Long nbgsId) {
        if (nbgsId == null) {
            nbgsId = -1l;
        }
        //修改检验
        String hql = " from Ejzh where kh.id = ? and nbgs.id = ?  ";
        return this.find(hql, khId, nbgsId).size() == 0 ? null : (List<Ejzh>) this.find(hql, khId, nbgsId);
    }

    public List<Map> findEjzhByKhId(Long khId) {
        String sql = "    select distinct id as \"ejzh.id\",jc as \"ejzh.jc\" " +
                "        from t_sys_ejzh   " +
                "          where deleted = 0 and kh_id = ? ";
        return (List<Map>) this.createSQLQuery(sql, khId).setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE).list();
    }

    public List<Ejzh> findEjzhByKhAndNbgsId(Long id, Long nbgsId) {
        return this.find(" from Ejzh where kh.id = ? ", id);
    }

    public List<Map> findKyyeByKh(Long khId) {
        String sql = "select nvl(sum(e.kyye),0) kyye from t_sys_ejzh e where e.kh_id = ? and e.deleted = 0";
        return (List<Map>) this.createSQLQuery(sql, khId).setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE).list();
    }

    public void insertEjzhKk(Long ejzhId, Double fpje, Long xsdId) {
        String sql = "insert into t_biz_ejzh_kk (deleted,ejzh_id,fpje,xsd_id) values (0,?,?,?)";
        executeSQL(sql, ejzhId, fpje, xsdId);
    }
}
